<template>
	<view class="work">
		<!-- <index-bar-fixed title="工作台"></index-bar-fixed> -->
		<view class="worktop" v-if="teamFlag">
			<view class="worktopmian">
				<view class="worktopl">
					{{teamName ? teamName : ''  }}
				</view>
				<view class="worktopr">
					<view class="qielogo">
						<image src="../../static/img/qie.png" mode=""></image>
					</view>
					<view class="qietit" @click="swTeam">
						切换团队
					</view>
				</view>
			</view>
		</view>
		<view class="m-t-24">
			<u-swiper :list="SwList" mode="rect"></u-swiper>
		</view>
		<view class="nav_list">
			<view class="mokuai" v-for="item in list" :key="item.title">
				<view class="mokuaitit">
					<view class="mokuaititmain">
						{{item.title}}
					</view>
				</view>
				<view class="mokuaimain">
					<view class="mokuaimainitem" v-for="it in item.arr" :class="{'opacity':it.opacity}" :key="it.name"
						@click.stop="go(it)">
						<view class="mokuailogo">
							<u-image :src="it.icon" width="88rpx" height="88rpx" mode=""></u-image>
						</view>
						<view class="mokuaitit">
							{{it.name}}
						</view>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		getTeamByUser,
		mySwiper,
		checkTeamUserOnTeam,
		HcemParams
	} from '@/api/index.js'
	import {
		checkToken,
		checkAuth,
		chooseTeamNow
	} from '@/utils/index.js'
	export default {
		data() {
			return {
				teamFlag: false,
				intoTeam: false,
				teamName: '',
				SwList: [],
				list: [{
						title: "患者关系管理",
						arr: [{
								name: "患者信息管理",
								opacity: false,
								icon: require("../../static/icon/work/workbench_me@2x.png")
							},
							{
								name: "分类标签管理",
								opacity: false,
								icon: require("../../static/icon/work/workbench_label.png")
							},
							{
								name: "复诊预约管理",
								opacity: false,
								icon: require("../../static/icon/work/fuzhen.png")
							},
						]
					},
					{
						title: "患者体验管理",
						arr: [{
								name: "患者口碑管理",
								opacity: true,
								icon: require(
									"../../static/icon/work/Workbench _ Patient word-of-mouth management@2x.png"
								)
							},
							{
								name: "患者投诉管理",
								opacity: true,
								icon: require(
									"../../static/icon/work/Workbench _ Patient Complaint Management@2x.png")
							}
						]
					},

					{
						title: "医生团队管理",
						arr: [{
								name: "我的团队",
								opacity: false,
								icon: require("../../static/icon/work/Workbench _ My team@2x.png")
							},
							{
								name: "团队管理",
								opacity: true,
								icon: require("../../static/icon/work/Workbench _ Team Management@2x.png")
							},
							{
								name: "权限管理",
								opacity: true,
								icon: require("../../static/icon/work/Workbench _ Rights Management@2x.png")
							},
						]
					},

					{
						title: "诊后疾病管理",
						arr: [{
								name: "诊后随访管理",
								opacity: false,
								icon: require(
									"../../static/icon/work/Workbench _ Post-diagnosis follow-up management@2x.png"
								)
							},
							{
								name: "诊后患教管理",
								opacity: true,
								icon: require("../../static/icon/work/Worktable _ Diagnosis and management@2x.png")
							},
							{
								name: "康复指导计划",
								opacity: true,
								icon: require(
									"../../static/icon/work/Workbench _ Rehabilitation Guidance Program@2x.png"
								)
							},
						]
					},

					// {
					// 	title: "数字化运营管理",
					// 	arr: [
					// 		{
					// 			name: "智能名片",
					// 			icon: require("../../static/icon/work/Workbench_SmartBusiness Card@2x.png")
					// 		}
					// 	]
					// },
					// {
					// 	title: "团队管理",
					// 	arr: [{
					// 			name: "我的团队",
					// 			icon: require("../../static/icon/work/Workbench_My team@2x.png")
					// 		}
					// 	]
					// },
					{
						title: "科室运营管理",
						arr: [{
								name: "聊天工具栏配置",
								opacity: false,
								icon: require('@/static/icon/work/Workbench _ Chat toolbar configuration@2x.png'),
							},
							{
								name: "科室HCEM管理",
								opacity: false,
								icon: require('@/static/icon/work/Workbench _ Department HCEM management@2x.png'),
							},
							{
								name: "智能名片",
								opacity: false,
								icon: require("../../static/icon/work/Workbench_SmartBusiness Card@2x.png")
							},
							{
								name: "问卷管理",
								opacity: false,
								icon: require('@/static/icon/work/Workbench _ Questionnaire Management@2x.png'),
							},
							// {
							// 	name: "权限管理",
							// 	icon: require("../../static/icon/work/Workbench_Rights Management@2x.png")
							// }
						]
					}
				],
				swiper: [
					// {
					// 	image: 'https://cdn.uviewui.com/uview/swiper/1.jpg',
					// 	title: '昨夜星辰昨夜风，画楼西畔桂堂东'
					// }
				],
				token: '',
				depId:null,
			}
		},
		computed: {

		},
		async onShow() {
			await chooseTeamNow()
			this.$on('hasTeam', this.hasTeam)
			let res = uni.getStorageSync('myTeam')
			if (res && res?.code == 200) {
				this.teamName = res.data.name
				this.teamFlag = true
				//console.log(res.data.id);
				this.isInTeam(res.data.id)
			} else {
				this.teamName = ''
				this.teamFlag = false
				uni.removeStorageSync('myTeam')
			}
			this.isHasDep()
			//console.log('1111');
		},
		mounted() {
			this.getSwiper()
		},
		onReady() {
			// let res = uni.getStorageSync('myTeam')
			// if (res && res?.code == 200) {
			// 	this.teamName = res.data.name
			// }
		},
		methods: {
			async getHcem() {
				let phoneNumber = uni.getStorageSync('userinfo').user.phonenumber
				const Admin = uni.getStorageSync('userinfo').roles
				if (phoneNumber && Admin.includes('deptAdmin')) {
					let res = await HcemParams(phoneNumber)
					if(res.code = 200){
						this.token = res.data.token
						this.depId = res.data.depId
					}else{
						this.$u.toast(res.msg,3000)
						return false
					}
					return true
					//console.log(res);
				}
			},
			isHasDep() {
				//科室hcem管理
				const Admin = uni.getStorageSync('userinfo').roles
				if (!Admin.includes('deptAdmin')) { //没有管理权限
					this.list.forEach(v => {
						//console.log(v);
						const findIndex = v.arr.findIndex(val => val.name == '科室HCEM管理')
						if (findIndex != -1) {
							v.arr = v.arr.filter(val => val.name != '科室HCEM管理')
						}
					})
				}
			},
			async isInTeam(teamId) {
				let res = await checkTeamUserOnTeam(teamId)
				//console.log(res.data,121212);
				if (!res.data) { //不在团队里把缓存清掉,一般用于自删在别人的团队里的自己
					this.teamName = ''
					this.teamFlag = false
					uni.removeStorageSync('myTeam')
				} else {

				}
				//console.log(userId,111);
			},
			async getSwiper() {
				let res = await mySwiper()
				if (res.code == 200) {
					this.SwList = res.data.map((v, k) => {
						return {
							image: v,
							title: k
						}
					})
				}
			},
			async hasTeam() {
				const token = uni.getStorageSync('token')
				if (!token) return
				// if (!checkToken()) return
				// if (!checkAuth()) return
				let r = uni.getStorageSync('myTeam')
				if (r && r?.code == 200) { //有缓存，说明选过了，也就最少有一个团队
					this.teamFlag = true
					return
				}
				let res = await getTeamByUser()
				if (res.code == 200) {
					if (res.rows.length > 0) {
						this.teamFlag = true
					} else {
						this.teamFlag = false
					}
				}
			},
			async go(item) {
				//console.log(item);
				let token = uni.getStorageSync('token')
				// if (!token) return
				if (!checkToken()) return
				if (!checkAuth()) return
				if (item.name == '患者信息管理') {
					//let r = uni.getStorageSync('myTeam')

					uni.navigateTo({
						url: '/pages/patient/index'
					})
				}
				if (item.name == '诊后随访管理') {
					uni.navigateTo({
						url: '/pages/follow/index'
					})
				}
				if (item.name == '复诊预约管理') {
					//this.$u.toast('暂未开放')
					uni.navigateTo({
						url: '/pages/index/fuzhen'
					})
				}
				if (item.name == '患者口碑管理') {
					//this.$u.toast('暂未开放')
					// uni.navigateTo({
					// 	url: '/pages/test/test'
					// })
				}
				if (item.name == '分类标签管理') {
					uni.navigateTo({
						url: '/pages/intel/cardList'
					})
				}
				if (item.name == '知识库管理') {
					// uni.navigateTo({
					// 	url: '/pages/patient/index'
					// })
				}
				if (item.name == '智能名片') {
					uni.navigateTo({
						url: '/pages/intel/index'
					})
				}
				if (item.name == '问卷管理') {
					uni.navigateTo({
						url: '/pages/wenjuan/index'
					})
				}
				
				if (item.name == '我的团队') {
					uni.navigateTo({
						url: '/pages/team/index'
					})
				}
				if (item.name == '聊天工具栏配置') {
					//http://localhost:5173/hcemClient/#/my/side
					// uni.navigateTo({
					// 	url: '/pages/index/topre'
					// })
					const Admin = uni.getStorageSync('userinfo').roles
					if (Admin.includes('deptAdmin')) {
						let  jump = await this.getHcem()
						// console.log(this.token,this.depId);
						// return
						if(jump){
							window.location.href = `https://hcem.wisdoc.cn/hcemClient/#/my/side?token=${this.token}&depId=${this.depId}`
							//window.location.href = `https://hcem.wisdoc.cn/hcemClient/#/home?token=123&depId=456`
						}else{
							window.location.href = `https://hcem.wisdoc.cn/hcemClient/#/login`
						}
					}
				}
				if (item.name == '科室HCEM管理') {
					const Admin = uni.getStorageSync('userinfo').roles
					if (Admin.includes('deptAdmin')) {
						let  jump = await this.getHcem()
						// console.log(this.token,this.depId);
						// return
						if(jump){
							window.location.href = `https://hcem.wisdoc.cn/hcemClient/#/home?token=${this.token}&depId=${this.depId}`
							//window.location.href = `https://hcem.wisdoc.cn/hcemClient/#/home?token=123&depId=456`
						}else{
							window.location.href = `https://hcem.wisdoc.cn/hcemClient/#/login`
						}
					}
				}
				if (item.name == '管理员管理') {
					// uni.navigateTo({
					// 	url: '/pages/team/index'
					// })
				}
				if (item.name == '权限管理') {
					// uni.navigateTo({
					// 	url: '/pages/team/index'
					// })
				}
			},
			swTeam() {
				uni.navigateTo({
					url: '/pages/team/switchTeam'
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.upToTop {
		//padding-top: 190rpx;
	}

	.opacity {
		opacity: .5;
	}

	.work {
		background-color: #f8f8f8;
		//padding-top: 180rpx;
		background: #F8F8F8;
		//position: absolute;
		width: 100%;
		padding-bottom: 100rpx;
		// width: 375px;
		// margin: 0 auto;
		// height: calc(100vh - 170rpx);

		.nav_list {
			padding-bottom: 80rpx;

			.mokuai {
				width: 100%;
				background-color: #fff;
				margin-top: 20rpx;

				.mokuaitit {
					width: 100%;
					border-bottom: 1rpx solid #e8e8e8;

					.mokuaititmain {
						width: 702rpx;
						margin: 0 auto;
						font-weight: 500;
						font-size: 26rpx;
						color: #666666;
						line-height: 74rpx;
					}
				}

				.mokuaimain {
					//width: 702rpx;
					//margin: 0 auto;

					.mokuaimainitem {
						//height: 100rpx;
						width: 100%;
						display: flex;
						//justify-content: space-between;
						align-items: center;

						.mokuailogo {
							margin-right: 24rpx;
							//margin-left: 26rpx;
						}

						.mokuaitit {
							//width: 624rpx;
							line-height: 100rpx;
							//font-weight: 500;
							font-size: 32rpx;
							color: #212121;
						}
					}
				}
			}

		}

		.worktopr {
			display: flex;
			width: 200rpx;
			height: 60rpx;
			background: #4DC0D9;
			border-radius: 12rpx 12rpx 12rpx 12rpx;
			margin-top: 24rpx;

			.qielogo {
				width: 32rpx;
				height: 32rpx;
				margin-top: 14rpx;
				margin-left: 20rpx;

				image {
					width: 100%;
					height: 100%;
				}
			}

			.qietit {
				font-weight: 500;
				margin-left: 20rpx;
				font-size: 30rpx;
				color: #FFFFFF;
				line-height: 60rpx;
			}
		}

		.worktop {
			margin-top: 20px;
			width: 750rpx;
			height: 108rpx;
			background: #FFFFFF;
			border-radius: 0rpx 0rpx 0rpx 0rpx;

			.worktopmian {
				width: 702rpx;
				margin: 0 auto;
				display: flex;
				justify-content: space-between;
				overflow: hidden;

				.worktopl {
					font-weight: 500;
					font-size: 32rpx;
					color: #212121;
					line-height: 108rpx;
				}

			}
		}
	}
</style>